package com.artesophy.library
{
	import flash.display.BitmapData;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.effects.Effect;
	import mx.messaging.management.Attribute;
	
	import spark.effects.Fade;
	import spark.primitives.BitmapImage;
	import spark.primitives.Rect;

	public class ImageFilters
	{
		private var mtrx_original:Array = [
			1,		0,		0,		0,		0,
			0,		1,		0,		0,		0,
			0,		0,		1,		0,		0,
			0,		0,		0,		1,		0
		];
		
		private var mtrx_grayscale:Array = [
			0.3, 	0.59, 	0.11, 	0,		0,
			0.3, 	0.59, 	0.11, 	0, 		0,
			0.3, 	0.59, 	0.11, 	0,		0,
			0, 		0, 		0, 		1, 		0
		];
		
		private var mtrx_sepia:Array = [
			0.3930000066757202, 0.7689999938011169,	0.1889999955892563, 0, 					0,	
			0.3490000069141388,	0.6859999895095825, 0.1679999977350235,	0, 					0,
			0.2720000147819519, 0.5339999794960022,	0.1309999972581863, 0, 					0,
			0, 					0, 					0, 					1, 					0,
			0, 					0, 					0, 					0, 					1
		];
		
		private var mtrx_bright:Array = [
			1.5,				0,					0,					0,					0,
			0,					1.5,				0,					0,					0,
			0,					0,					1.5,				0,					0,
			0,					0,					0,					1,					0
		];
		
		private var mtrx_darken:Array = [
			0.6,	0,		0,		0,		0,
			0,		0.6,	0,		0,		0,
			0,		0,		0.6,	0,		0,
			0,		0,		0,		1,		0
		];
		
		private var mtrx_club:Array = [
			1.2,	0,		0.4,	0,		0,
			0,		0.8,	0,		0,		0,
			0.2,	0,		1.3,	0,		0,
			0,		0,		0,		1,		0
		];
		
		private var mtrx_mood:Array = [
			1,		0,		0.4,	0,		0,
			0,		0.8,	0,		0,		0,
			0,		0,		1.2,	0,		0,
			0,		0,		0,		1,		0
		];
		
		public var currentFilterName:String;
		
		public function ImageFilters()
		{
			this.currentFilterName = "";
		}
		
		
		public var originalImage:BitmapData;
		
		public function preApplyFilter(image:BitmapImage, filterName:String = null):void{
			
			
			var eff_fadeIn:Fade = new Fade;
			
			this.currentFilterName = filterName;
			this.originalImage = image.bitmapData;
			
			switch(filterName){
				
			}
			
			var imageFilter:ColorMatrixFilter = getFilterObjByName(filterName);
			
			image.alpha = 0;
			image.filters = [imageFilter];
			eff_fadeIn.target = image;
			eff_fadeIn.duration = 500;
			eff_fadeIn.alphaFrom = 0; eff_fadeIn.alphaTo = 1;
			eff_fadeIn.play();
			
			
			
		}
		
		private function getFilterObjByName(name:String):ColorMatrixFilter{
			var filterMtrx:Array;
			switch(name){
				case 'GRAYSCALE':
					filterMtrx = mtrx_grayscale;
					break;
				case 'SEPIA':
					filterMtrx = mtrx_sepia;
					break;
				case 'BRIGHT':
					filterMtrx = mtrx_bright;
					break;
				case 'DARKEN':
					filterMtrx = mtrx_darken;
					break;
				case 'CLUB':
					filterMtrx = mtrx_club;
					break;
				case 'MOOD':
					filterMtrx = mtrx_mood;
					break;
				default:
					filterMtrx = mtrx_original;
					break;
			}
			
			return (new ColorMatrixFilter(filterMtrx));
		}
		
		
		public function getFilteredImage():BitmapData{
			
			var filteredImageData:BitmapData = this.originalImage;
			filteredImageData.applyFilter(
				this.originalImage, 
				new Rectangle(0, 0, filteredImageData.width, filteredImageData.height), 
				new Point(0,0), 
				getFilterObjByName(this.currentFilterName)
			);
			
			return filteredImageData;	
		}
		
		public function changeAlpha(alpha:Number):void{
			
		}
		
	}
}